<?php
namespace Common\Logic;

/**
 * 消息推送逻辑
 * @modify wenhui 2017-03-16    创建
 */
class NewsPushLogic extends \Common\Controller\BaseController {

    public $statusPush = array(
        'zh_cn' => array(
            HOTEL_WEB => 'WEB端',
            HOTEL_APP => 'APP端',
            NEWSTATUS_NOTREAD => '未读',
            NEWSTATUS_READ => '已读',
            NEWSTATUS_DELE => '删除',
            NOT_CHECK => '未查',
            HAVE_CHECK => '已查',
        ),
        'en_us' => array(
            HOTEL_WEB => 'WEB',
            HOTEL_APP => 'APP',
            NEWSTATUS_NOTREAD => 'Unread',
            NEWSTATUS_READ => 'Read',
            NEWSTATUS_DELE => 'delete',
            NOT_CHECK => 'Not checked',
            HAVE_CHECK => 'Already checked',
        ),
        'zh_tw' => array(
            HOTEL_WEB => 'WEB端',
            HOTEL_APP => 'APP端',
            NEWSTATUS_NOTREAD => '未讀',
            NEWSTATUS_READ => '已讀',
            NEWSTATUS_DELE => '删除',
            NOT_CHECK => '未查',
            HAVE_CHECK => '已查',
        ),
    );
	/**
     * 获取推送列表
     * @modify wenhui 2017-03-16    创建
     * @modify zhangzhen 2017-06-05  钟点房返回格式
     * @param  int   $hotelId    客栈id
     * @param  int   $userId     用户id
     * @param  int   $pageIndex     页码
     * @param  int   $pageSize     页数
     * @param  string   $keys     是否根据keys过滤数据
     * @param  int   $type     根据 type 过滤数据 0=预定消息 1=提前退房消息
     * @return array $data       数据
     */
    public function getNewsList($hotels, $userId, $pageIndex = null, $pageSize = null, $keys = null, $type = null) {
    	
        $hotelNewsPushModel = D('HotelNewspush');
        $hotelNewsPushRelateModel = D('HotelNewspushRelate');
        $HotelRoomAuthorityModel = D('HotelRoomAuthority');
        $adminHotelModel = D('AdminHotel');
        $hotelEntityModel = D('HotelEntity');

        // 获取关联数据
        $newsPushRelateInfo = $hotelNewsPushRelateModel->getNewsRelateByHotelAndUserId($hotels, $userId);
        if (empty($newsPushRelateInfo)) {
            return array(
                        'newsList' => array(),
                        'page' => array(
                                        'current' => 0,
                                        'size' => 0,
                                        'total' => 0,
                                        'pageTotal' => 0,
                                ),
                        );
        }

        // 获取推送消息数据
        $newsIds = array_unique(array_column($newsPushRelateInfo, 'hotelnewspush_id'));
        $newsPushInfo = $hotelNewsPushModel->getNewsListByNewsId($newsIds);
        if ($newsPushInfo == false) {
            return array(
                        'newsList' => array(),
                        'page' => array(
                                        'current' => 0,
                                        'size' => 0,
                                        'total' => 0,
                                        'pageTotal' => 0,
                                ),
                        );
        }

        // 组装数据
        $newsPushData = array();
        foreach ($newsPushRelateInfo as $key => $newsPushRelateItem) {
            foreach ($newsPushInfo as $key => $newsPushItem) {
                if ($newsPushRelateItem['hotelnewspush_id'] == $newsPushItem['id']) {
                    $newsTypeName = $newsPushRelateItem['newstype'] == 0 ? $this->statusPush[LANG_SET]['NEWSTATUS_NOTREAD'] : $this->statusPush[LANG_SET]['NEWSTATUS_READ'];
                    // 客栈名称
                    $hotelEntityInfo = $hotelEntityModel->getInfo($newsPushItem['hotelentity_id']);
                    // 过滤管理员对该房间没有权限的消息
                    $HotelRoomAuthorityInfo = $HotelRoomAuthorityModel->getInfoByUserRoom($userId, $newsPushItem['room']);
                    // 检查用户是否是客栈拥有者
                    $adminInfo = $adminHotelModel->getInfosByAdmin($userId, 1, $newsPushItem['hotelentity_id']);
                    $isAdminUser = empty($adminInfo) ? false : true;
                    if (!empty($HotelRoomAuthorityInfo) || $isAdminUser) {
                        $newsPushDataItem = array(
                            'hotelName' => $hotelEntityInfo['name'],
                            'hotel' => $newsPushItem['hotelentity_id'],
                            'orderRoomId' => $newsPushItem['orderroom_id'],
                            'roomName' => $newsPushItem['roomname'],
                            'startTime' => date('Y/m/d H:i:s', strtotime($newsPushItem['startdate'])),
                            'nights' => $newsPushItem['night'],
                            'hour' => $newsPushItem['hour'],
                            'month' => $newsPushItem['month'],
                            'newsPushId' => $newsPushItem['id'],
                            'areacode' => $newsPushItem['areacode'],
                            'mobile' => $newsPushItem['mobile'],
                            'name' => $newsPushItem['name'],
                            'orderInfoId' => $newsPushItem['orderinfo_id'],
                            'roomId' => $newsPushItem['room'],
                            'type' => $newsPushItem['type'],
                            'newsType' => $newsPushRelateItem['newstype'],
                            'newsTypeName' => $newsTypeName,
                            'newsPushRelateId' => $newsPushRelateItem['id'],
                            'createTime' => date('YmdHis', strtotime($newsPushRelateItem['createtime'])),
                        );
                        array_push($newsPushData,  $newsPushDataItem);
                    }
                }
            }
            unset($newsPushItem);
        }
        unset($newsPushRelateItem);

        if (!is_null($keys) || !is_null($type)) {
            // 筛选数据
            foreach ($newsPushData as $key => $newsPushItem) {
                if (!is_null($type)) {
                    if ($type != $newsPushItem['type']) {
                        unset($newsPushData[$key]);
                        continue;
                    }
                }

                if (!is_null($keys)) {
                    // 拼接字符串
                    $searchString = '**' . $newsPushItem['roomName'] . '**'  . $newsPushItem['name'] . '**' . $newsPushItem['mobile'];
                    if (strpos($searchString, $keys) === false) {
                        unset($newsPushData[$key]);
                        continue;
                    }
                }
            }
            unset($newsPushItem);
        }

        $newsPushData = array_values($newsPushData);
        if (empty($newsPushData)) {
            return array(
                    'newsList' => array(),
                    'page' => array(
                                    'current' => 0,
                                    'size' => 0,
                                    'total' => 0,
                                    'pageTotal' => 0,
                            ),
                    );
        }

        // 排序 未读排在前面 降序
        $newsPushData = my_sort($newsPushData, 'createTime', SORT_DESC);

        if (!is_null($pageIndex) && !is_null($pageSize)) {
           // 分页
            $newsPushData = createPageInfo($newsPushData, $pageIndex, $pageSize);
            $pageInfo = array(
                'current' => $newsPushData['current'],
                'size' => $newsPushData['size'],
                'total' => $newsPushData['total'],
                'pageTotal' => $newsPushData['pageTotal'],
            );

            $newsPushData = array('newsList' => $newsPushData['data'], 'page' => $pageInfo);
        }

    	return $newsPushData;
    }

    /**
     * 保存修改
     * @modify wenhui 2017-03-16    创建
     * @param  int   $pushList     修改数据
     * @return bool
     */
    public function saveNews($pushList, $userId = null, $channel = 0) {

        $hotelNewsPushRelateModel = D('HotelNewspushRelate');
        $hotelNewsPushRecordModel = D('HotelNewspushRecord');
        $adminLoginModel = D('AdminLogin');
        $hotelNewsPushRelateModel->startTrans();
        $commit = true;

        foreach ($pushList as $key => $pushListItem) {
            $newsPushInfo = $hotelNewsPushRelateModel->saveNews($pushListItem, $userId);
            if ($newsPushInfo == false) {
                $commit = false;
                break;
            }

            if ($pushListItem['type'] == 1 || $pushListItem['type'] == 2) {
                // 获取用户名称
                $name = $adminLoginModel->getInfo($userId)['name'];
               // 添加操作记录
                $newsPushInfo = $hotelNewsPushRecordModel->addNewsHistory($pushListItem['newsPushId'], $pushListItem['type'], $userId, $channel, $name);
                if ($newsPushInfo == false) {
                    $commit = false;
                    break;
                }
            }
        }
        unset($pushListItem);

        if ($commit) {
            $hotelNewsPushRelateModel->commit();
            // 记录redis数据， 用于Cli/Controller/SocketController  public function startNewsPush() 定时推送
            $hotelNewsPushRelateModel->saveNewsRedis($userId);
            return true;
        } else {
            $hotelNewsPushRelateModel->rollback();
            returnJson(500, 506, 'error');
        }     
    }

     /**
     * 获取推送操作历史记录
     * @modify wenhui 2017-03-16    创建
     * @param  int   $newsId   
     * @return array $data       数据
     */
    public function newsHistory($newsId) {

        $hotelNewspushRecordModel = D('HotelNewspushRecord');

        $newsHistroyInfo = $hotelNewspushRecordModel->getNewsHistoryByNewsId($newsId);
        if (empty($newsHistroyInfo)) {
            returnJson(403, 801, 'error');
        }

        // 组装数据
        $newsHistroyData = array();
        foreach ($newsHistroyInfo as $key => $newsHistroyItem) {
            $channel = $newsHistroyItem['channel'] == 0 ? $this->statusPush[LANG_SET]['HOTEL_WEB'] : $this->statusPush[LANG_SET]['HOTEL_APP'];
            $operateType = $newsHistroyItem['creatorstep'] == 1 ? $this->statusPush[LANG_SET]['NEWSTATUS_READ'] : $this->statusPush[LANG_SET]['NEWSTATUS_DELE'];
            $newsHistroyDataItem = array(
                'startTime' => $newsHistroyItem['updatetime'],
                'operator' => $newsHistroyItem['name'],
                'channel' => $channel,
                'operateType' => $operateType,
            );

            array_push($newsHistroyData, $newsHistroyDataItem);
        }
        unset($newsHistroyItem);

        return $newsHistroyData;
    }

     /**
     * 设置声音
     * @modify wenhui 2017-03-16    创建
     */
    public function setVoice($hotelId, $type) {
        $hotelEntityModel = D('HotelEntity');
        $hotelEntityInfo = $hotelEntityModel->saveInfo($hotelId, null, null, null, null, $type);
        if ($hotelEntityInfo == false) {
            returnJson(500, 506, 'error');
        }

        return true;
    }
     /**
     * 获取数据
     * @modify wenhui 2017-03-16    创建
     */
    public function getVoice($hotelId) {
        $hotelEntityModel = D('HotelEntity');
        $hotelEntityInfo = $hotelEntityModel->getInfo($hotelId);
        if (empty($hotelEntityInfo)) {
            return false;
        }

       return $hotelEntityInfo;
    }

    /**
     * 获取查房消息
     * @modify wenhui 2018-02-127     创建
     * @param  array   $hotelId    客栈id
     * @param  int   $pageIndex     页码
     * @param  int   $pageSize     页数
     * @param  string   $keys     是否根据keys过滤数据
     * @param  int | array  $type     0=未查 1=已查 2=知道了
     * @param  int  $userIdsend     发起查房任务用户id
     * @param  bool  $validateAuthority   校验有查房权限
     * @return array $returnData       数据
     */
    public function getCheckInfo($hotelId, $keys, $pageIndex, $pageSize, $type = null, $userIdsend = null, $validateAuthority = false) {
        $hotelRoomCheckModel = D('HotelRoomCheck');
        $hotelRoomCheckCostModel = D('HotelRoomCheckCost');
        $hotelEntityModel = D('HotelEntity');
        $orderRoomModel = D('OrderRoom');
        $adminHotelModel = D('AdminHotel');
        $hotelRoomAuthorityModel = D('HotelRoomAuthority');

        $userId = getUserId();
        //检查是否为客栈管理员
        if (!is_array($hotelId)) {
            $hotelId = explode(',', $hotelId);
        }
        $adminHotel = $adminHotelModel->getInfoByAdminHotel($userId, $hotelId);
        $adminHotel = getArrayValueAsIndex($adminHotel, 'hotelentity_id');

        // 获取客栈信息
        $hotelEntityInfo = $hotelEntityModel->getInfoByHotelId($hotelId);
        $hotelEntityInfo = getArrayValueAsIndex($hotelEntityInfo, 'id');

        $roomCheckInfo = $hotelRoomCheckModel->getInfoByhotelId($hotelId);
        // 获取查房消费物品数据
        $checkIds = array_column($roomCheckInfo, 'id');
        $hotelRoomCheckCostInfo = $hotelRoomCheckCostModel->getInfoByCheckId($checkIds);
        $hotelRoomCheckCostInfo = getArrayValueAsIndex($hotelRoomCheckCostInfo, 'hotelroomcheck_id');
        // 获取子订单数据
        $usbOrderId = array_column($roomCheckInfo, 'orderroom_id');
        $orderRoomInfo = $orderRoomModel->getInfo($usbOrderId);
        $orderRoomInfo = getArrayValueAsIndex($orderRoomInfo, 'id');

        // 校验有查房权限 与 房间权限
        $authorityResult = array();
        $hotelRoomAuthorityInfo = array();

        $roomIds = array_column($orderRoomInfo, 'roominfo_id');
        $hotelRoomAuthorityInfo = $hotelRoomAuthorityModel->getInfoByUserRoom($userId, $roomIds, $hotelId);
        $hotelRoomAuthorityInfo = getArrayValueAsIndex($hotelRoomAuthorityInfo, 'adminlogin_id', true);
        if ($validateAuthority === true) {
            $adminAuthorityModel = D('AdminAuthority');
            $authorityResult = $adminAuthorityModel->getInfoByAdminHotelAuthority($userId, $hotelId, 27);
            $authorityResult = getArrayValueAsIndex($authorityResult, 'hotelentity_id');
        }

        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelRoomCostData = $hotelLogic->getCheckRoomCost($hotelId);
        $hotelRoomCostData = getArrayValueAsIndex($hotelRoomCostData, 'id');
        // 处理数据
        $data = array();
        foreach ($roomCheckInfo as $key => $roomCheck) {
            $orderRoom = $orderRoomInfo[$roomCheck['orderroom_id']];

            if (!is_null($type)) {
                if (!is_array($type)) {
                    $type = explode(',', $type);
                }
                if (!in_array($roomCheck['type'], $type)) {
                    unset($roomCheckInfo[$key]);
                    continue;
                }
            }

            //不是客栈创建人员，需要过滤
            if ($adminHotel[$roomCheck['hotelentity_id']] && $adminHotel[$roomCheck['hotelentity_id']]['role'] != HOTEL_ROLE_OWNER) {

                // app端才进行 过滤没有查房权限
                if ($validateAuthority === true) {
                    if (empty($authorityResult)) {
                        unset($roomCheckInfo[$key]);
                        continue;
                    }
                    if (!empty($authorityResult)) {
                        if (!isset($authorityResult[$roomCheck['hotelentity_id']])) {
                            unset($roomCheckInfo[$key]);
                            continue;
                        }
                    }
                }

                // 过滤没有房间权限
                if (empty($hotelRoomAuthorityInfo)) {
                    unset($roomCheckInfo[$key]);
                    continue;
                }
                if (!empty($hotelRoomAuthorityInfo)) {
                    $adminRoomAuthorityInfo = $hotelRoomAuthorityInfo[$userId];
                    $adminRoomAuthorityInfo = getArrayValueAsIndex($adminRoomAuthorityInfo, 'roominfo_id');
                    if (!isset($adminRoomAuthorityInfo[$orderRoom['roominfo_id']])) {
                        unset($roomCheckInfo[$key]);
                        continue;
                    }
                }
            }

            // 只有掌柜与发起者才看到消息
            if (!empty($userIdsend)) {
                if ($adminHotel[$roomCheck['hotelentity_id']]['role'] != HOTEL_ROLE_OWNER && $roomCheck['adminlogin_id_send'] != $userIdsend) {
                    unset($roomCheckInfo[$key]);
                    continue;
                }
            }

            // 筛选数据
            if (!is_null($keys)) {
                // 拼接字符串
                $searchString = '**' . $roomCheck['roomname'] . $roomCheck['name'] . '**';
                if (strpos($searchString, $keys) === false) {
                    unset($roomCheckInfo[$key]);
                    continue;
                }
            }

            $checkCostIds = array_column($hotelRoomCheckCostInfo[$roomCheck['id']], 'hotelroomcost_id');
            $checkCost = '';
            if (!empty($checkCostIds)) {
                foreach ($checkCostIds as $key => $checkCostId) {
                    $checkCost .= isset($hotelRoomCostData[$checkCostId]) ? $hotelRoomCostData[$checkCostId]['name'] . ',' : '';
                }
                unset($checkCostId);
                $checkCost = substr($checkCost,0,strlen($checkCost)-1);
            }

            $hotelName = $hotelEntityInfo[$roomCheck['hotelentity_id']]['name'];
            $data[] = array(
                'id' =>  $roomCheck['id'],
                'hotel' =>  $roomCheck['hotelentity_id'],
                'hotelName' =>  $hotelName,
                'roomName' =>  $roomCheck['roomname'],
                'name' =>  $roomCheck['name'],
                'remark' =>  $roomCheck['remark'],
                'endTime' =>  $roomCheck['updatetime'],
                'orderInfoId' =>  $orderRoom['orderinfo_id'],
                'orderRoomId' =>  $orderRoom['id'],
                'roomId' =>  $orderRoom['roominfo_id'],
                'typeName' =>  $roomCheck['type'] == 0 ? $this->statusPush[LANG_SET]['NOT_CHECK'] : $this->statusPush[LANG_SET]['HAVE_CHECK'],
                'type' =>  $roomCheck['type'],
                'createStamp' =>  strtotime($roomCheck['createtime']),
                'createTime' =>  $roomCheck['createtime'],
                'checkCost' =>  $checkCost,
            );
        }

        // 排序 未读排在前面 降序
        $data = my_sort($data, 'createStamp', SORT_DESC);

        // 分页
        $data = createPageInfo($data, $pageIndex, $pageSize);
        $pageInfo = array(
            'current' => $data['current'],
            'size' => $data['size'],
            'total' => $data['total'],
            'pageTotal' => $data['pageTotal'],
        );

        $returnData = array('roomCheck' => $data['data'], 'page' => $pageInfo);

        return $returnData;
    }

    /**
     * 方法抛异常
     * 获取查房消息详情
     * @modify wenhui 2018-02-127     创建
     * @param  int   $hotelId    id
     * @param  int   $checkId    查房记录id
     * @return array $returnData       数据
     */
    public function getCheckDetail($hotelId, $checkId) {
        $hotelRoomCheckModel = D('HotelRoomCheck');
        $hotelRoomCheckCostModel = D('HotelRoomCheckCost');
        $orderRoomModel = D('OrderRoom');
        $hotelEntityModel = D('HotelEntity');
        $adminLoginModel = D('AdminLogin');

        $roomCheckInfo = $hotelRoomCheckModel->getCheckInfoById($checkId);
        if (empty($roomCheckInfo)) {
            throw new \Exception('500,515', 515);
        }

        $roomCheckCostInfo = $hotelRoomCheckCostModel->getInfoByCheckId($checkId);
        $orderRoomInfo = $orderRoomModel->getInfo($roomCheckInfo['orderroom_id']);
        // 获取查房消费物品资源
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelRoomCostData = $hotelLogic->getCheckRoomCost($hotelId, true);
        $hotelRoomCostData = getArrayValueAsIndex($hotelRoomCostData, 'id');

        $startDate = date('Y-m-d', strtotime($orderRoomInfo[0]['startdate']));
        $endDate = date('Y-m-d', strtotime($orderRoomInfo[0]['enddate']));
        $hotelName = $hotelEntityModel->getName($roomCheckInfo['hotelentity_id']);
        $adminName = $adminLoginModel->getName($roomCheckInfo['adminlogin_id_check']);
        $roomCostIds = array_column($roomCheckCostInfo, 'hotelroomcost_id');
        $checkCost = array();
        foreach ($roomCostIds as $key => $roomCostId) {
            $checkCost[] = array(
                'checkCostId' => $roomCostId,
                'name' => $hotelRoomCostData[$roomCostId]['name'],
            );
        }
        unset($roomCostId);

        // 处理数据
        $returnData = array(
            'hotelName' => $hotelName,
            'roomName' => $roomCheckInfo['roomname'],
            'name' => $roomCheckInfo['name'],
            'remark' => $roomCheckInfo['remark'],
            'type' => $roomCheckInfo['type'],
            'operatorId' => $roomCheckInfo['adminlogin_id_check'],
            'operator' => isset($adminName) ? $adminName : '',
            'startDate' => $startDate,
            'endDate' => $endDate,
            'endDate' => $endDate,
            'checkCost' => $checkCost,
        );

        return $returnData;
    }

    /**
     * 方法抛异常
     * 获取查房消息详情
     * @modify wenhui 2018-02-127     创建
     * @param  int   $hotelId    客栈id
     * @param  int   $checkId    查房记录id
     * @param  array   $roomCostId    查房消费物品id
     * @param  string   $remark    详情
     * @return array $returnData       数据
     */
    public function editCheck($hotelId, $checkId, $roomCostId, $remark) {
        $hotelRoomCheckModel = D('HotelRoomCheck');
        $hotelRoomCheckCostModel = D('HotelRoomCheckCost');
        $orderRoomCheckRecordModel = D('OrderRoomCheckRecord');

        $roomCheckInfo = $hotelRoomCheckModel->getCheckInfoById($checkId);
        if (empty($roomCheckInfo)) {
            throw new \Exception('500,515', 515);
            return false;
        }

        $userId = getUserId();
        // 检查房间权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        if (!$adminAuthorityLogic->checkIsAdmin($userId, $hotelId)) {
            $orderRoomModel = D('OrderRoom');
            $hotelRoomAuthorityModel = D('HotelRoomAuthority');
            $orderRoomInfo = $orderRoomModel->getInfo($roomCheckInfo['orderroom_id']);
            $hotelRoomAuthorityInfo = $hotelRoomAuthorityModel->getInfoByUserRoom($userId, $orderRoomInfo[0]['roominfo_id'], $hotelId);
            if (empty($hotelRoomAuthorityInfo)) {
                throw new \Exception('403,202', 202);
                return false;
            }
        }

        if (!empty($roomCheckInfo['adminlogin_id_check']) && $roomCheckInfo['adminlogin_id_check'] != $userId) {
            throw new \Exception('403,713', 713); // 已检查
            return false;
        }

        // 开始事务
        $hotelRoomCheckModel->startTrans();

        $commit = true;
        $type = $roomCheckInfo['type'] == 0 ? 1 : null;
        $saveResult = $hotelRoomCheckModel->saveInfo($checkId, $userId, $remark, $type);
        if ($saveResult == false) {
            $commit = false;
        }

        $roomCheckCostInfo = $hotelRoomCheckCostModel->getInfoByCheckId($checkId);
        $oldRoomCostIds = array_column($roomCheckCostInfo, 'hotelroomcost_id');

        $addRoomCostIds = array_unique(array_diff($roomCostId, $oldRoomCostIds));
        $deleteRoomCostIds = !empty($roomCostId) ? array_unique(array_diff($oldRoomCostIds, $roomCostId)) : $oldRoomCostIds;
        // 添加
        if ($commit && !empty($addRoomCostIds)) {
            $addData = array();
            foreach ($addRoomCostIds as $key => $roomCostIdItem) {
                $addData[] = array(
                    'hotelroomcheck_id' => $checkId,
                    'hotelroomcost_id' => $roomCostIdItem,
                    'creator' => $userId,
                    'operator' => $userId,
                );
            }
            unset($roomCostIdItem);

            $addResult = $hotelRoomCheckCostModel->addAllInfo($addData);
            if ($addResult == false) {
                $commit = false;
            }
        }

        // 删除
        if ($commit && !empty($deleteRoomCostIds)) {
            $saveResult = $hotelRoomCheckCostModel->deleteInfo($checkId, $deleteRoomCostIds);
            if ($saveResult == false) {
                $commit = false;
            }
        }

        // 生成订单操作记录
        if ($commit) {
            $orderRecordModel = D('OrderRecord');
            $orderRecordId = $orderRecordModel->addInfo($roomCheckInfo['orderinfo_id'], 1, null, null, null, null, null, null, 1);
            if (empty($orderRecordId)) {
                $commit = false;
            }

            if ($commit) {
                $oldRoomCostIds = !empty($oldRoomCostIds) ? implode(',', $oldRoomCostIds) : '';
                $roomCostId = !empty($roomCostId) ? implode(',', $roomCostId) : '';
                if ($roomCostId == $oldRoomCostIds) {
                    $oldRoomCostIds = '';
                    $roomCostId = '';
                }

                $oldRemark = $roomCheckInfo['remark'];
                if ($remark == $oldRemark) {
                    $remark = '';
                    $oldRemark = '';
                }

                $orderCheckRoomRecords = array(
                    array(
                        'orderrecord_id' => $orderRecordId,
                        'checktype' => 2,
                        'hotelroomcost_id' => $roomCostId,
                        'remark' => $remark,
                        'checktype_before' => $roomCheckInfo['type'],
                        'hotelroomcost_id_before' => $oldRoomCostIds,
                        'remark_before' => $oldRemark,
                        'type' => 2,
                    ),
                );

                // 添加查房操作记录
                if (!empty($orderCheckRoomRecords)) {
                    $changeResult = $orderRoomCheckRecordModel->addInfo($orderCheckRoomRecords);
                    if (!$changeResult) {
                        $commit = false;
                    }
                }
            }
        }

        // 第一次查房完毕向web发起查房服务
        if ($commit && $roomCheckInfo['type'] == 0) {
            $orderLogic = A('Common/Order', 'Logic');
            $pushResult = $orderLogic->pushCheckRoomTask($roomCheckInfo['hotelentity_id'], '', 'app', $roomCheckInfo['adminlogin_id_send']);
        }

        if ($commit) {
            $hotelRoomCheckModel->commit();
            return true;
        } else {
            $hotelRoomCheckModel->rollback();
            throw new \Exception('500,506', 506);
            return false;
        }
    }

    /**
     * 方法抛异常
     * 获取查房消息详情
     * @modify wenhui 2018-02-127     创建
     * @param  int   $checkId    查房记录id
     * @return array $returnData       数据
     */
    public function deleteCheck($checkId) {
        $hotelRoomCheckModel = D('HotelRoomCheck');
        $hotelRoomCheckCostModel = D('HotelRoomCheckCost');

        // 开始事务
        $hotelRoomCheckModel->startTrans();
        $commit = true;
        $saveResult = $hotelRoomCheckModel->deleteInfo($checkId, null, null, null, IS_DELETE);
        if ($saveResult == false) {
            $commit = false;
        }

        if ($commit) {
            $saveResult = $hotelRoomCheckCostModel->deleteInfo($checkId);
            if ($saveResult == false) {
                $commit = false;
            }
        }

        if ($commit) {
            $hotelRoomCheckModel->commit();
            return true;
        } else {
            $hotelRoomCheckModel->rollback();
            throw new \Exception('500,510', 510);
            return false;
        }
    }
}